//
//  PartCollectView.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2019/1/4.
//  Copyright © 2019 Gox_WallyDiao. All rights reserved.
//

import UIKit

protocol PartCollectViewDelegate:NSObjectProtocol {
    func PartCollectViewClick(index:Int, str:String)
}

class PartCollectView: UIView {
    
    weak var delegate:PartCollectViewDelegate?
    
    fileprivate let PartCollectCellID = "PartCollectCell"
    
    var model = PartSeasonModel()
    var modelList: PartSeasonModel? {
        didSet {
            guard let model = modelList else { return }
            self.model = model
            self.collectionView.reloadData()
        }
    }

    fileprivate lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout.init()
        layout.itemSize = CGSize.init(width: SCREEN_WIDTH / 4, height: 37)
//        layout.headerReferenceSize = CGSize.init(width: SCREEN_WIDTH, height: 55)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        let collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.register(PartCollectCell.self, forCellWithReuseIdentifier: PartCollectCellID)
        collectionView.delegate = self
        collectionView.dataSource = self
        return collectionView
    }()
    
    init(frame: CGRect, height:CGFloat) {
        super.init(frame: frame)
        self.collectionView.frame = FRAME_CGFloat(0, 0, SCREEN_WIDTH, height)
        self.collectionView.backgroundColor = WhiteBackColor
        self.addSubview(self.collectionView)
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

//  MARK: - ###### 协议方法 ######
extension PartCollectView:UICollectionViewDelegate,UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.model.area.count 
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PartCollectCellID, for: indexPath) as! PartCollectCell
        cell.label.text = self.model.area[indexPath.row] as? String
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        delegate?.PartCollectViewClick(index: indexPath.row, str: self.model.area[indexPath.row] as! String)
    }
    
}
